<?php if ( ! defined('ABSPATH')) exit('restricted access'); /** stop direct script access */

/**
 * Customized re-usable functions
 *
 * @package 	WordPress
 * @subpackage 	WelCare
 * @author		WP Nukes Developers Team
 * @link		http://www.wpnukes.com
 * @since		Version 1.0
 */

get_template_part('libs/shortcodes_class');
get_template_part('libs/shortcode_editor');
get_template_part('libs/post_types');
include('libs/metaboxes/meta_boxes.php');

/** If the popup for shortcode inserter is loaded */
if(isset($_GET['fw_editor_action']) && $_GET['fw_editor_action'] == 'shortcode')
{
	get_template_part('libs/sc-editor');exit;
}

/** Instatiate the shortcodes */
$FW_Shortcodes = new FW_Shortcodes;
$FW_Shortcodes->add_shortcode();

/** Add action control the contact form submission */
add_action('nuke_submit_contact_form', 'nuke_submit_contact_form');
function nuke_submit_contact_form()
{
	$FW_Ajax_Handler = new FW_Ajax_Handler;
	$FW_Ajax_Handler->save_contact();
}

/**
 * Prints the post according to the required format stored into postmeta
 *
 * @param	string	$style	value of the style to print
 * @return string	Prints the html of the provided style.
 */
function wl_style_switcher($style = '', $block = 'block')
{
	global $_webnukes;

	$general_settings = $_webnukes->get_settings('general_settings');
	$style = ( ! $style) ? $_webnukes->kvalue($general_settings, 'blog_style') : $style;
	$class = ($style != 'style3') ? 'middle' : '';
	$img_class = array('style1'=>'snap', 'style2'=>'snap', 'style3'=>'center');
	$img_size = ($style == 'style3') ? 'post-image2' : 'post-image1';?>
	
	<div <?php post_class($block); ?>>
        <article class="blog_listing">
            <!-- Post heading -->
            <div class="head_section <?php echo $class; ?>">
                <div class="post_date">
                    <div class="day_month">
                        <span class="day"><?php the_time('d'); ?></span>
                        <span class="month"><?php the_time('M'); ?></span>
                    </div>
                    <span class="post_year"><?php the_time('Y'); ?></span>
                </div>
                <h1><a href="<?php the_permalink() ?>" title="<?php the_title_attribute() ?>"><?php the_title() ?></a></h1>
                <?php if($style == 'style3'): ?>	
                    <div class="contact_bar">
                        <ul>
                            <li><a class="doctor_icon" href="<?php echo HOME_URL ?>?author=<?php the_author_meta( 'ID' ) ?>"><?php the_author() ?></a></li>
                            <li class="comments_icon"><?php comments_popup_link(  __( '0 Comments', THEME_NAME ), __( '1 Comment', THEME_NAME ), __( '% Comments', THEME_NAME ) ); ?></li>
                            <li class="iphone_articles articles_icon"><?php the_category(', '); ?></li>
                        </ul>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Post content  --> 			
            <?php if($style != 'style3'): ?>
				<?php if(has_post_thumbnail()) :?>
				<div class="block">
				<?php endif;?>
				
            	<ul class="blog_menu <?php echo ($style == 'style2') ? 'pull_left' : 'pull_right';?>">
                    <li class="ico-doctor"><a href="<?php echo HOME_URL;?>?author=<?php the_author_meta( 'ID' ) ?>"><?php the_author() ?></a></li>
                    <li class="ico-date"><a href="<?php echo get_month_link( get_the_time('Y'), get_the_time('m') ); ?>"><?php the_time('F d, Y'); ?></a></li>
                    <?php if(has_term(wp_get_post_terms(get_the_ID(), 'wpnukes_gallery', array('fields'=>'ids')), 'wpnukes_gallery') || has_category()): ?>
                    	<?php if(get_post_type() == 'wpnukes_galleries'):?>
                    		<li class="ico-tags"><?php echo get_the_term_list(get_the_ID(), 'wpnukes_gallery', '', ', '); ?></li>
                        <?php else: ?>
                        	<li class="ico-tags"><?php the_category(', '); ?></li>
                        <?php endif; ?>
                    <?php endif; ?>
                    <li class="ico-commints-fill"><?php comments_popup_link(  __( '0 Comments', THEME_NAME ), __( '1 Comment', THEME_NAME ), __( '% Comments', THEME_NAME ) ); ?></li>
                    <?php if(has_tag()):?>
                    	<li class="ico-tags2"><?php the_tags(''); ?></li>
                    <?php endif; ?>
                    <li class="ico-rss-feeds last-child"><?php post_comments_feed_link(__('Subscribe RSS', THEME_NAME)); ?></li>
                </ul>
                <!-- Post Thumbnail -->
	            <?php if(has_post_thumbnail()): the_post_thumbnail($img_size, array('class'=>$img_class[$style])); ?>
					</div>
				<?php endif;?>	
			<?php endif;?>

            <div class="details">
            	<!-- Post Thumbnail -->
                <?php if(has_post_thumbnail() && $style == 'style3') the_post_thumbnail($img_size, array('class'=>$img_class[$style])); ?>
				<?php the_excerpt(); ?>
            </div>
            <!-- details end -->
			<hr class="sp" />
        </article>
    </div>
<?php
}

/**
 * returns the rating of doctor page
 *
 * @return	string	HTML of the requested rating.
 */
function get_the_breadcrumb()
{
	global $_webnukes;
	$queried_object = get_queried_object();
	$breadcrumb = '';
	if ( ! is_home() )
	{
		$breadcrumb .= '<li><a href="'.home_url().'"><img width="14" height="13" src="'.THEME_URL.'/images/home_icon.png" alt=""></a></li>';
		/** If category or single post */
		if(is_category())
		{
			$breadcrumb .= '<li><a href="'.get_category_link(get_query_var('cat')).'">'.single_cat_title('', FALSE).'</a></li>';
		}
		elseif(is_tax())
		{
			$breadcrumb .= '<li><a href="'.get_term_link($queried_object).'">'.$queried_object->name.'</a></li>';
		}
		/** If WP pages */
		elseif(is_page())
		{
			global $post;
			if($post->post_parent)
			{
                $anc = get_post_ancestors($post->ID);
                foreach($anc as $ancestor)
				{
                    $breadcrumb .= '<li>'.get_the_title($ancestor).'</li>';
                }
				
            }else $breadcrumb .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
		}
		elseif (is_singular())
		{
			if($category = wp_get_object_terms(get_the_ID(), array('category', 'wpnukes_gallery')))
			{
				$breadcrumb .= '<li><a href="'.get_term_link($category[0]).'">'.$category[0]->name.'</a></li>';
				$breadcrumb .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
			}
		}
		elseif(is_tag()) $breadcrumb .= '<li><a href="'.get_term_link($queried_object).'">'.single_tag_title('', FALSE).'</a></li>'; /**If tag template*/
		elseif(is_day()) $breadcrumb .= '<li><a href="">'.__('Archive for ', THEME_NAME).get_the_time('F jS, Y').'</a></li>'; /** If daily Archives */
		elseif(is_month()) $breadcrumb .= '<li><a href="' .get_month_link(get_the_time('Y'), get_the_time('m')) .'">'.__('Archive for ', THEME_NAME).get_the_time('F, Y').'</a></li>'; /** If montly Archives */
		elseif(is_year()) $breadcrumb .= '<li><a href="'.get_year_link(get_the_time('Y')).'">'.__('Archive for ', THEME_NAME).get_the_time('Y').'</a></li>'; /** If year Archives */
		elseif(is_author()) $breadcrumb .= '<li><a href="'. esc_url( get_author_posts_url( get_the_author_meta( "ID" ) ) ) .'">'.__('Archive for ', THEME_NAME).get_the_author().'</a></li>'; /** If author Archives */
		elseif(is_search()) $breadcrumb .= '<li>'.__('Search Results for ', THEME_NAME).get_search_query().'</li>'; /** if search template */
		else $breadcrumb .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; /** Default value */
	}

	return '<ul>'.$breadcrumb.'</ul>';
}

/**
 *return the changes text
 */

function wl_excerpt_more($more)
{
	return '<a href="' . get_permalink() . '" class="read_more">'.__('Read More', THEME_NAME).'</a>'; 
}

/**replace default [...] exerpt to Read more..*/
add_filter('excerpt_more', 'wl_excerpt_more');


/**
 * returns the rating of doctor page
 *
 * @param	int		$name	unique name of the field of rating
 * @param	string	$key	serialize array of rating values stores in usersmeta
 *
 * @return	string	HTML of the requested rating.
 */

function wl_doctor_rating($name = '1', $key = '')
{
	global $_webnukes;
	if($key):
		$rating = range(1, 5);
		$doctor_industry = unserialize($key);
		$disabled = in_array($_SERVER['REMOTE_ADDR'], (array)$_webnukes->kvalue($doctor_industry, 'ips')) ? 'disabled="disabled"' : ''; 
		 //printr($rating);
		 foreach($rating as $val):
			$required = ($val == 1) ? ' required' : '';
			$checked = (round($_webnukes->kvalue($doctor_industry, 'avg')) == $val) ? 'checked="checked"' : '';?>
			
			<input name="star<?php echo $name; ?>" value="<?php echo $val; ?>" type="radio" class="stars <?php echo $required; ?>" <?php echo $disabled; ?> <?php echo $checked; ?>/>
		 <?php endforeach;
	endif;
}

/**
 * returns the formatted form of the comments
 *
 * @param	array	$args		an array of arguments to be filtered
 * @param	int		$post_id	if form is called within the loop then post_id is optional
 *
 * @return	string	Return the comment form
 */
function wl_comment_form( $args = array(), $post_id = null )
{
	global $user_identity, $id;
	
	if ( null === $post_id )
		$post_id = $id;
	else
		$id = $post_id;

	$commenter = wp_get_current_commenter();

	$req = get_option( 'require_name_email' );
	$aria_req = ( $req ? " aria-required='true'" : '' );
	
	$comment_author = (esc_attr( $commenter['comment_author'] )) ? esc_attr( $commenter['comment_author'] ) : __('Enter Name', THEME_NAME);
	$comment_email = (esc_attr( $commenter['comment_author_email'] )) ? esc_attr( $commenter['comment_author_email'] ) : __('Enter Email', THEME_NAME);
	$comment_url= (esc_attr( $commenter['comment_author_url'] )) ? esc_attr( $commenter['comment_author_url'] ) : __('Enter Website', THEME_NAME);
	
	/** Comment form custom fields */
	$author_val = __('Enter Name', THEME_NAME);
	$email_val = __('Enter Email', THEME_NAME);
	$url_val = __('Enter Website', THEME_NAME);

	$fields =  array(
		'author' => '<input type="text" name="author" class="text_short name" value="'.$comment_author.'" onfocus="if(this.value == \'' .$author_val .'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'' .$author_val .'\'; }" />',

		'email' => '<input type="text" name="email" class="text_short email" value="'.$comment_email.'" onfocus="if(this.value == \'' .$email_val .'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'' .$email_val .'\'; }" />',

		'url' => '<input type="text" name="url" class="text website" value="'.$comment_url.'" onfocus="if(this.value == \'' .$url_val .'\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'' .$url_val .'\'; }" />',
	);

	$required_text = sprintf( ' ' . __('Required fields are marked %s','heavens_corner'), '<span class="required">*</span>' );
	$defaults = array(
		'fields' => apply_filters( 'comment_form_default_fields', $fields ),
		'comment_field' => '<ul class="forms"><li><textarea id="comment" name="comment" cols="45" rows="5" aria-required="true"></textarea></li></ul>',
		'must_log_in' => '<ul class="forms"><li class="txt">&nbsp;</li><li>' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.','heavens_corner' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</li></ul>',
		'logged_in_as'         => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>','heavens_corner' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
		'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.','heavens_corner' ) . ( $req ? $required_text : '' ) . '</p>',
		'comment_notes_after'  => '<ul class="forms"><li class="txt">&nbsp;</li><li>' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s','heavens_corner' ), '<p><code>' . allowed_tags() . '</code></p>' ) . '</li></ul>',
		'id_form' => 'commentform',
		'id_submit' => 'submit',
		'title_reply' => __( 'Leave a Comment','heavens_corner' ),
		'title_reply_to' => __( 'Leave a Reply to %s','heavens_corner' ),
		'cancel_reply_link' => __( 'Cancel reply','heavens_corner' ),
		'label_submit' => __( 'Submit','heavens_corner' ),
	);

	$args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );?>
		<?php if ( comments_open() ) : ?>
			<?php do_action( 'comment_form_before' ); ?>
			<div class="replycomment" id="respond">
               <div id="errormessages"></div>
				<h3 class="colr heading"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>

				<?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
					<?php echo $args['must_log_in']; ?>
					<?php do_action( 'comment_form_must_log_in_after' ); ?>
				<?php else : ?>
					<form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>">
						<?php do_action( 'comment_form_top' ); ?>
						<?php if ( is_user_logged_in() ) : ?>
							<?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
							<?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
						<?php else : ?>
							<?php echo $args['comment_notes_before']; ?>
							<?php
							do_action( 'comment_form_before_fields' );
							foreach ( (array) $args['fields'] as $name => $field ) {
								echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
							}
							do_action( 'comment_form_after_fields' );
							?>
						<?php endif; ?>
						<?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
						<?php echo $args['comment_notes_after']; ?>
						<ul class="forms">
                        	<li class="txt">&nbsp;</li>
                            <li>
                                <input  name="submit" type="submit" value="Submit" class="button" id="<?php echo esc_attr( $args['id_submit'] ); ?>" style="width:100px;" />
							    <?php comment_id_fields( $post_id ); ?>
                            </li>
                        </ul>
						<?php do_action( 'comment_form', $post_id ); ?>
					</form>
				<?php endif; ?>
			</div><!-- #respond -->
			<?php do_action( 'comment_form_after' ); ?>
		<?php else : ?>
			<?php do_action( 'comment_form_comments_closed' ); ?>
		<?php endif; ?>
	<?php
}


/**
 * A callback function of comments listing
 * 
 * @param	object	$comment	containg a list of comments
 * @param	array	$args		An array of arguments to merge with defaults.
 * @param	int		$depth		Containing the reply depth level.
 */

function wl_list_comments($comment, $args, $depth)
{

	$GLOBALS['comment'] = $comment; 
	if(get_comment_type() == 'comment'):?>

	<li>
		<div class="comment_box" id="comment-<?php comment_ID();?>">
			<div class="up">
				<div class="snap"><?php echo get_avatar( $comment, $size='43'); /** get avatar */?></div>
				<div class="upSection">
					<strong><?php printf( '%s', get_comment_author_link() ); /** print comment author */?></strong>
					<span><?php comment_date( get_option( 'date_format' ) ); _e(' at ', THEME_NAME); comment_time( 'g:i a' ); /** print date time */?></span>
				</div>
			</div>
            
			<div class="down">
				<?php /** check if this comment author not have approved comments befor this */
				if($comment->comment_approved == '0' ) : ?>
					<em><?php /** print message below */
					_e( 'Your comment is awaiting moderation.', THEME_NAME ); ?></em>
					<br />
				<?php endif; ?>
				<?php comment_text(); /** print our comment text */ ?>

				<?php /** check if thread comments are enable then print a reply link */
				comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
			</div>

			<?php if ($depth >= 2) { ?><span class="connect"></span><?php } ?>
		</div>
	<?php
	endif;
}

/**
 * Function to show banners on subpages
 * 
 * @param	string	$page		slug of the page to show banner
 * @return	string	html of subpages banner
 */

function fw_subpages_banner($subpage)
{
	global $_webnukes;

	$blog = $_webnukes->get_settings('subpages_banner');

	if($_webnukes->kvalue($blog, 'status') == 'active') :
		
		/** Store values in their relevant template keys */
		$tmpls = array_reduce((array) $_webnukes->kvalue($blog, 'DYNAMIC'), create_function('$new_tpl, $v', '$new_tpl[$v["section"]] = $v; return $new_tpl;'));

		$is_meta = (in_array($subpage, array('single', 'page', 'full_width', 'doctor_listing'))) ? true : false;
		$post_meta = get_post_meta(get_the_ID(), 'header_image_meta', true);

		if($is_meta && ! empty($post_meta['upload_image']))
		{
			$subpages = $post_meta;
			$subpages['status'] = 'active';
		}else $subpages = ($_webnukes->kvalue($tmpls, $subpage)) ? $_webnukes->kvalue($tmpls, $subpage) : array();

		/** check whether current tpl found in the array */
		if($subpages):
			if($_webnukes->kvalue($subpages, 'status') == 'active'):?>            
                <div class="page-banner">
                    <div class="banner-image">
						<img src="<?php echo $_webnukes->kvalue($subpages, 'upload_image');?>" alt="<?php echo $_webnukes->kvalue($subpages, 'title');?>" />
					</div>
                    <div class="content white">
                        <h1><?php echo $_webnukes->kvalue($subpages, 'title'); /** Function to check key into given array to avoid notices, theme_functions.php*/?></h1>
                        <p><?php echo $_webnukes->kvalue($subpages, 'text'); ?></p>
                    </div>
                </div>
			<?php endif;
		else:?>
			<?php if($image = $_webnukes->kvalue($blog, 'default_image')):?>
				<div class="page-banner">
					<div class="banner-image">
                    	<img src="<?php echo $image;?>" alt="<?php echo $_webnukes->kvalue($blog, 'default_title');?>" />
                    </div>
                    <div class="content white">
                        <h1><?php echo $_webnukes->kvalue($blog, 'default_title'); /** Function to check key into given array to avoid notices, theme_functions.php*/?></h1>
                        <p><?php echo $_webnukes->kvalue($blog, 'default_text'); ?></p>
                    </div>
				</div>
			<?php endif;?>
		<?php
		endif;?>
	<?php
	endif;
}


/**
 * change the default style of post pagination
 *
 * @param	array	$defaults	an array of default arguments to create post pagination
 * @return	string	Returns the formatted post pagination arguments
 */
function custom_nextpage_links($defaults)
{
	$args = array(
		'next_or_number'=>'next',
		'nextpagelink'=>'<span class="nxt"></span>',
		'previouspagelink'=>'<span class="prv"></span>'
	);
	
	$r = wp_parse_args($args, $defaults);
	return $r;
}

add_filter('wp_link_pages_args', 'custom_nextpage_links');


/**
 * this function is used to check whether the given number is singular, plural or null and return the string accordingly
 *
 * @param	int		$name	Number to be checked
 * @param	string	$none	if provided number is zero then return this string.
 * @param	string	$single	if provided number is one then return this string.
 * @param	string	$plural	if provided number is greater than 1 then return this string.
 *
 * @return	string	Returns the string according to the number.
 */
function fw_singular_plural($number = 0 , $none = '', $single = '', $plural = '')
{
	if(!$number) return $none;
	elseif($number == 1) return $single;
	else return $plural;
}


/**
 * this function either prints or return the pagination of any archive/listing page.
 *
 * @param	array	$args	Array of arguments
 * @param	bolean	$echo	whether print or return the output.
 *
 * @return	string	Prints or return the pagination output.
 */
function the_pagination($args = array(), $echo = 1)
{
	
	global $wp_query;
	
	$default =  array('base' => str_replace( 99999, '%#%', esc_url( get_pagenum_link( 99999 ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ),
						'total' => $wp_query->max_num_pages, 'next_text' => __('Next', THEME_NAME), 'prev_text' => __('Previous', THEME_NAME));
						
	$args = wp_parse_args($args, $default);			
	
	$pagination = '<div class="pagination">'.paginate_links($args).'</div>';
	
	if(paginate_links(array_merge(array('type'=>'array'),$args)))
	{
		if($echo) echo $pagination;
		return $pagination;
	}
}

/**
 * prints the header of wpnukes gallery frontend
 *
 * @param	bolean	$cat	Whether function is called for gallery category or single view.
 * @return	string	prints the html of gallery header.
 */
function wpnukes_gallery_header($cat = true)
{
	global $post, $wp_query;
	$total_values = array_sum((array)get_post_meta($post->ID, 'wpnukes_gallery_count', true));
	$object_term =  get_queried_object();?>

    <div class="quick_links_div">
        <div class="whide_btn_div">	
			<strong><a href="javascript:void(0);"><?php echo ($cat) ? number_format($wp_query->found_posts) : number_format($total_values);?></a></strong>
            <?php if($cat):?>
            	<span><a href="javascript:void(0);"> <?php echo fw_singular_plural($wp_query->found_posts, __('No Gallery Found', THEME_NAME), __('Gallery', THEME_NAME), __('Galleries', THEME_NAME));?></a></span>
			<?php else:?>
				<span><a href="javascript:void(0);"> <?php echo fw_singular_plural($wp_query->found_posts, __('Nothing Found', THEME_NAME), __('Images & Videos', THEME_NAME), __('Images & Videos', THEME_NAME));?></a></span>
			<?php endif;?>
        </div>
		
        <div class="gallery-filter-icons">
            <div class="changelayout">
                <a href="javascript:void(0);" class="grid"><img src="<?php echo get_template_directory_uri();?>/images/gallery_icon2.png" alt="" /></a>
                <a href="javascript:void(0);" class="list"><img src="<?php echo get_template_directory_uri();?>/images/gallery_icon1.png" alt="" /></a>
            </div>
            <div class="filterimagevideo">
                <a href="javascript:void(0);" class="video_filter"><img src="<?php echo get_template_directory_uri();?>/images/video_quick_link.png" alt="videos" /></a>
                <a href="javascript:void(0);" class="picture_filter"><img src="<?php echo get_template_directory_uri();?>/images/wallpapers.png" alt="pictures" /></a>
                <a href="javascript:void(0);" class="all"><img src="<?php echo get_template_directory_uri();?>/images/all.png" alt="all" /></a>
            </div>
        </div>
		
        <div class="gallery-filer-dropdown">
            <select name="gallery_categories" id="select-gallery-filter" class="dropbox">
                <option value="0"><?php _e('Select Gallery Category', THEME_NAME);?></option>
                <?php foreach((array)get_terms('wpnukes_gallery') as $gterm):?>
                    <?php $selected_term = ($object_term->term_id == $gterm->term_id) ? 'selected="selected"' : '';?>
                    <option value="<?php echo get_term_link($gterm);?>" <?php echo $selected_term; ?>><?php echo $gterm->name;?></option>
                <?php endforeach;?>
            </select>
        </div>
    </div>
<?php
}

function tweets($ret) {
  	$ret = preg_replace("/((www\.|(http|https)+\:\/\/)[_.A-Za-z0-9-]+\.[A-Za-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/", '<a href="$1" target="_blank" >$1</a>', $ret);
	$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
  	return $ret;
}


function kvalue( $var, $key, $def = '' )

{

	if( !$var ) return false;



	if( is_object( $var ) && isset( $var->$key ) ) return $var->$key;

	elseif( is_array( $var ) && isset( $var[$key] ) ) return $var[$key];

	elseif( $def ) return $def;

	else return false;

}
